package com.ymatou.iapi.shoppingcart.service;

import org.json.JSONArray;
import org.json.JSONObject;

import com.ymt.base.YmatouBaseCall;
import com.ymttest.utils.EnvSetup;

public class GetShoppingCartListCall extends YmatouBaseCall{
	
	private static String opurl = "api/shoppingcart/getshoppingcartlist";

	public GetShoppingCartListCall() {
		super(EnvSetup.getSystemUrl("shoppingCartiapi"), opurl, "GET", "PARAM");
	}

	@Override
	public void addDefHeader() {
		client.addHeader("Content-Type", "application/json");
	}
	
	/**
	 * 获取Result
	 * 
	 * @return Result
	 */
	public JSONObject getResult() {
		JSONObject Data = null;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			Data = jo.getJSONObject("result");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Data;
	}
	
	/**
	 * 获取status
	 * 
	 * @return status
	 */
	public int getStatus() {
		int status = 0;
		try {
			status = new JSONObject(getReturnData()).getInt("status");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return status;
	}
	
	/**
	 * 获取有效买手list
	 * 
	 * @return status
	 */
	public JSONArray getValidList() {
		JSONArray array = null;
		try {
			array = this.getResult().getJSONArray("list");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return array;
	}
	
	/**
	 * 获取有效商品list
	 * 
	 * @return status
	 */
	public JSONArray getValidProdList() {
		JSONArray prodList_return = new JSONArray();
		try {
			JSONArray array = this.getResult().getJSONArray("list");
			
			for(int i = 0; i < array.length(); i++) {
				JSONArray promotionGroupArray = array.getJSONObject(i).getJSONArray("promotionGroup");
				
				for (int j = 0; j < promotionGroupArray.length(); j++) {
					JSONArray prodList = promotionGroupArray.getJSONObject(j).getJSONArray("prodList");
					
					for (int k = 0; k < prodList.length(); k++) {
						prodList_return.put(prodList.getJSONObject(k));
					}
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return prodList_return;
	}
	
	/**
	 * 获取有效商品list
	 * 
	 * @return status
	 */
	public JSONArray getInvalidProdList() {
		JSONArray prodList_return = new JSONArray();
		try {
			JSONArray array = this.getResult().getJSONArray("invalidProdList");
			
			prodList_return = array;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return prodList_return;
	}
	
	/**
	 * 根据sellerid获取购物车对应的买手信息
	 * @param sellerId
	 * @return status
	 */
	public JSONObject getSellerInfo(int sellerId) {
		JSONObject object = null;
		try {
			for (int i =0; i < this.getValidList().length(); i++) {
				if (sellerId == this.getValidList().getJSONObject(i).getJSONObject("sellerInfo").getInt("id")) {
					object = this.getValidList().getJSONObject(i).getJSONObject("sellerInfo");
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object;
	}
}
